package com.ruoyi.web.controller.official;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.official.domain.*;
import com.ruoyi.official.dto.OfficialMessageDTO;
import com.ruoyi.official.query.OfficialDeviceQuery;
import com.ruoyi.official.query.OfficialNewsQuery;
import com.ruoyi.official.query.OfficialProductQuery;
import com.ruoyi.official.service.*;
import com.ruoyi.official.vo.OfficialProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 官网
 *
 * @author yuanqiji
 * @date 2025/03/24
 **/
@RestController
@RequestMapping("official/website/")
public class OfficialWebsiteController extends BaseController {
    @Autowired
    private IOfficialCategoryService officialCategoryService;
    @Autowired
    private IOfficialBannerService officialBannerService;
    @Autowired
    private IOfficialDeviceService officialDeviceService;
    @Autowired
    private IOfficialProductService officialProductService;
    @Autowired
    private IOfficialNewsService officialNewsService;
    @Autowired
    private IOfficialMessageService officialMessageService;

    /**
     * 轮播图列表
     */
    @GetMapping("banner/list")
    public AjaxResult list() {
        OfficialBanner officialBanner = new OfficialBanner();
        officialBanner.setStatus("1");
        List<OfficialBanner> list = officialBannerService.selectOfficialBannerList(officialBanner);
        return AjaxResult.success(list);
    }

    /**
     * 获取所有的分类
     */
    @GetMapping("category/list")
    public AjaxResult all() {
        return AjaxResult.success(officialCategoryService.selectOfficialAll());
    }

    /**
     * 分页获取产品列表
     */
    @GetMapping("product/list")
    public TableDataInfo list(OfficialProductQuery query) {
        OfficialProduct officialProduct = new OfficialProduct();
        officialProduct.setCategoryId(query.getCategoryId());
        officialProduct.setProductNameZh(query.getProductNameZh());
        officialProduct.setProductNameEn(query.getProductNameEn());
        startPage();
        List<OfficialProduct> list = officialProductService.selectOfficialProductList(officialProduct);
        return getDataTable(list);
    }

    /**
     * 获取产品详情
     */
    @GetMapping(value = "product/{id}")
    public AjaxResult getProductInfo(@PathVariable("id") Long id) {
        OfficialProductVO vo = officialProductService.selectOfficialProductById(id);
        officialProductService.addVisitNumber(id);
        return success(vo);
    }

    /**
     * 分页获取所有的设备
     */
    @GetMapping("device/list")
    public TableDataInfo deviceList(OfficialDeviceQuery query) {
        OfficialDevice officialDevice = new OfficialDevice();
        officialDevice.setDeviceNameZh(query.getDeviceNameZh());
        officialDevice.setDeviceNameEn(query.getDeviceNameEn());
        startPage();
        List<OfficialDevice> list = officialDeviceService.selectOfficialDeviceList(officialDevice);
        return getDataTable(list);
    }

    /**
     * 获取设备详情
     */
    @GetMapping(value = "device/{id}")
    public AjaxResult getDeviceInfo(@PathVariable("id") Long id) {
        return success(officialDeviceService.selectOfficialDeviceById(id));
    }

    /**
     * 分页获取新闻列表
     */
    @GetMapping("/list")
    public TableDataInfo newsList(OfficialNewsQuery query) {
        OfficialNews officialNews = new OfficialNews();
        officialNews.setType(query.getType());
        officialNews.setTitleZh(query.getTitleZh());
        officialNews.setTitleEn(query.getTitleEn());
        startPage();
        List<OfficialNews> list = officialNewsService.selectOfficialNewsList(officialNews);
        return getDataTable(list);
    }

    /**
     * 获取新闻详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getNewsInfo(@PathVariable("id") Long id) {
        officialNewsService.addVisitNumber(id);
        return success(officialNewsService.selectOfficialNewsById(id));
    }

    /**
     * 新增留言
     */
    @PostMapping("message")
    public AjaxResult add(@RequestBody OfficialMessageDTO dto) {
        OfficialMessage officialMessage = new OfficialMessage();
        BeanUtils.copyProperties(dto, officialMessage);
        return toAjax(officialMessageService.insertOfficialMessage(officialMessage));
    }


}
